package com.cysyz.sqlparser;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.util.AddAliasesVisitor;
import net.sf.jsqlparser.util.TablesNamesFinder;

import java.io.StringReader;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by admin on 2015-3-31.
 */
public class Test{
    public Test() throws SQLException {
    }

    public static void main(String[] args) {
        try {
            Statement stat = new CCJSqlParserManager().parse(new StringReader("SELECT\n" +
                    "        cp.province,\n" +
                    "        cc.city,\n" +
                    "        sc.name catagoryName,\n" +
                    "        cl.feature,\n" +
                    "        org.name orgname,\n" +
                    "        cl.price,\n" +
                    "        cl.favourprice,\n" +
                    "        cl.classpk,\n" +
                    "        cl.year\n" +
                    "    FROM\n" +
                    "        class_list cl ,\n" +
                    "        organization org,\n" +
                    "        cf_province cp,\n" +
                    "        cf_city cc,\n" +
                    "        sys_catagory sc  \n" +
                    "    WHERE\n" +
                    "        cl.org_pk=org.pk  \n" +
                    "        AND sc.categorypk = cl.categorypk \n" +
                    "        AND cp.provincepk = cl.province \n" +
                    "        AND cc.citypk = cl.citypk \n" +
                    "        AND 1=1 \n" +
                    "        \n" +
                    "        AND org_pk = 25559 and org_name = '大幅度释放'"));
            System.out.println();

            Select select = (Select) CCJSqlParserUtil.parse("select a,b,c from test");
            final AddAliasesVisitor instance = new AddAliasesVisitor();
            select.getSelectBody().accept(instance);

            TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
            List<String> tableList = tablesNamesFinder.getTableList((Select) stat);
            tablesNamesFinder.visit(new Table("haha"));

            ((Select)stat).getSelectBody().accept(new SelectVisitor() {
                @Override
                public void visit(PlainSelect plainSelect) {
                    System.out.println(plainSelect.getFromItem().getAlias());

                    List<Join> joins = plainSelect.getJoins();
                    if (joins != null) {
                        for (Join join : joins) {
                            join.getRightItem().accept(new FromItemVisitor() {
                                @Override
                                public void visit(Table table) {
                                    System.out.println(table.getName());
                                }

                                @Override
                                public void visit(SubSelect subSelect) {

                                }

                                @Override
                                public void visit(SubJoin subJoin) {

                                }

                                @Override
                                public void visit(LateralSubSelect lateralSubSelect) {

                                }

                                @Override
                                public void visit(ValuesList valuesList) {

                                }
                            });

                        }
                    }
                }

                @Override
                public void visit(SetOperationList setOperationList) {

                }

                @Override
                public void visit(WithItem withItem) {

                }
            });
        } catch (JSQLParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
